﻿using System;
using System.Collections.Generic;
using System.IO;
using CrateMayhem.Common.Data;
using Windows.ApplicationModel;
using Windows.Storage;

namespace CrateMayhem.WinRT.Data
{
    public class WorldRepository : WorldRepositoryBase
    {
        static WorldRepository()
        {
            if (DesignMode.DesignModeEnabled) return;

            var uri = new Uri("ms-appx:///Assets/BoxWorld.txt");
            var file = StorageFile.GetFileFromApplicationUriAsync(uri).AsTask().Result;
            var stream = file.OpenStreamForReadAsync().Result;

            using (var reader = new StreamReader(stream))
            {
                var data = new List<string>();

                var line = reader.ReadLine();

                while (line != null)
                {
                    if (String.IsNullOrWhiteSpace(line))
                    {
                        Worlds.Add(Create(data));
                        data.Clear();
                    }
                    else
                    {
                        data.Add(line);
                    }

                    line = reader.ReadLine();
                }

                Worlds.Add(Create(data));
            }
        }
    }
}
